﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sausage.Data;

namespace Sausage.Domain
{
    public enum RoleType
    {
        //总段管理员
        DistrictAdmin = 1,
        
        //分段管理员
        SectionAdmin = 2,

        //养护管理员
        MaintainUser = 3,

        //项目管理员
        ProjectUser = 4,

        //未知Role
        Unknow = -1 
    }

    public class RoleHelper
    {
        private static readonly Guid DistrictAdminRoleID = new Guid("8D633F3E-CE7D-46B4-A439-4AB7B38F2C04");
        private static readonly Guid SectionAdminRoleID = new Guid("72D59329-0A3A-45EA-8118-9F0EEC6BD252");
        private static readonly Guid MaintainUserRoleID = new Guid("D535DED5-0C2E-4994-91DA-E2149EFE7948");
        private static readonly Guid ProjectUserRoleID = new Guid("8B266001-1D92-42B7-9E44-E83F77022755");

        public static RoleType GetRoleType(Role role)
        {
            if (role == null)
                return RoleType.Unknow;
            if(role.RoleID == DistrictAdminRoleID)
                return RoleType.DistrictAdmin;
            if (role.RoleID == SectionAdminRoleID)
                return RoleType.SectionAdmin;
            if(role.RoleID == MaintainUserRoleID)
                return RoleType.MaintainUser;
            if(role.RoleID == ProjectUserRoleID)
                return RoleType.ProjectUser;
            return RoleType.Unknow;
        }

        public static List<Role> GetRoleByUser(User targetUser)
        {
            using (var context = new SausageEntities())
            {
                return (from role in context.Role
                 join userRole in context.UserRole on role.RoleID equals userRole.RoleID
                 join user in context.User on userRole.UserID equals user.UserID
                 where user.UserID == targetUser.UserID
                 select role).ToList();
            }
        }

        public static Role GetDefaultRoleByUser(User targetUser)
        {
            var list = GetRoleByUser(targetUser);
            return list.FirstOrDefault();
        }
    }
}
